///
/// ArmyBodger 3
/// (c)2010 richyp
///
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using ArmyBodger.Core.Utilities;

namespace ArmyBodger.Core {
    /// <summary>
    /// A Class to parse equipment XML files, both Game System and Army Book versions
    /// </summary>
    public class EquipmentParser {
        /// <summary>
        /// Parse the specfied equipment XML file
        /// </summary>
        /// <param name="file">The XML file to parse</param>
        /// <returns>A collection of Equipment objects</returns>
        public EquipmentCollection ParseEquipmentFile(string file) {
            EquipmentCollection results = new EquipmentCollection();
            if( ! File.Exists(file) ) {
                throw new Exception(file + " does not exist");
            }
            XDocument doc = null;
            try {
                doc = XDocument.Load(file);
            } catch { 
                throw new Exception(file + " is not an XML file");
            }
            if( doc.Root.Name != "equipment")  {
                MessageBox.Show(file + " is not a valid equipment file");
                return results;
            }
            foreach (XElement categoryElement in doc.XPathSelectElements("equipment/category")) {
                string category = "Unknown";
                if (categoryElement.Attribute("name") == null) {
                    Console.WriteLine("Warning: in " + file + " there is an unamed category, using Unknown");
                } else {
                    category = categoryElement.Attribute("name").Value;
                }
                foreach (XElement equipmentElement in categoryElement.XPathSelectElements("item")) {
                    Equipment eq = new Equipment();
                    eq.Name = equipmentElement.Element("name").Value;
                    if (equipmentElement.Element("description") != null) {
                        eq.Description = equipmentElement.Element("description").Value;
                    } else {
                        eq.Description = "No description";
                    }
                    eq.Category = category;
                    foreach (XElement bonusElement in equipmentElement.XPathSelectElements("bonuses/attributes/attribute")) {
                        Bonus bonus = new Bonus(
                            bonusElement.Element("name").Value,
                            int.Parse(bonusElement.Element("value").Value),
                            (BonusType)Enum.Parse(typeof(BonusType), Helpers.ConvertToSentenceCase(bonusElement.Element("value").Attribute("type").Value))
                            );
                        eq.Bonuses.Add(bonus);
                    }
                    results.Add(eq);
                }
            }
            return results;
        }
    }
}
